package com.cloudlinkscm.loms.services.base.notify.domain.notifier.resolver.request;

import com.cloudlinkscm.loms.framework.dao.BizDataInterface;
import com.cloudlinkscm.loms.services.base.notify.dao.TenantEmailAccountDao;
import com.cloudlinkscm.loms.services.base.notify.domain.notifier.RequestResolver;
import com.cloudlinkscm.loms.services.base.notify.dto.email.Cc;
import com.cloudlinkscm.loms.services.base.notify.dto.email.Ccls;
import com.cloudlinkscm.loms.services.base.notify.exception.biz.CclsInvalidFormatException;
import com.cloudlinkscm.loms.services.base.notify.model.EmailNotification;
import com.cloudlinkscm.loms.services.base.notify.model.entity.EmailMessage;
import com.cloudlinkscm.loms.services.base.notify.model.entity.TenantEmailAccount;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * email消息解析器
 *
 * @author : tac
 * @date : 2017/6/14
 */

@Component
public class EmailNotificationRequestResolver implements RequestResolver<EmailMessage, EmailNotification> {
    private TenantEmailAccountDao accountDao;
    private ObjectMapper mapper = new ObjectMapper();

    @Autowired
    public EmailNotificationRequestResolver(TenantEmailAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public EmailNotification doResolve(EmailMessage message) {
        EmailNotification notification = new EmailNotification();
        setMailInfo(notification, message);
        setSMTP(notification);
        return notification;
    }

    private void setMailInfo(EmailNotification notification, EmailMessage message) {
        notification.setSubject(message.getSubject());
        notification.setRecipient(message.getRecipientMail());
        notification.setSsl(message.getSsl());

        if(BooleanUtils.isTrue(message.getUrl())){
            //todo:: 使用restTemplate bean
            String html = new RestTemplate().getForObject(message.getUrlHead(), String.class);
            notification.setMsg(html);
        }else{
            notification.setMsg(message.getBody());
        }

        if(StringUtils.isNoneBlank(message.getRecipientCcls())){
            notification.setCcls(parseCcls(message));
        }
    }

    private void setSMTP(EmailNotification notification) {
        TenantEmailAccount account = getAccountByTenantId();

        notification.setAccount(account.getAccount());
        notification.setPassword(account.getPassword());
        notification.setHostName(account.getHostName());
        if(notification.getSsl()){
            notification.setPort(account.getSslPort());
        }else {
            notification.setPort(account.getPort());
        }

        notification.setSuffix(account.getMailType());
    }

    private TenantEmailAccount getAccountByTenantId() {
        TenantEmailAccount o = new TenantEmailAccount();
        o.setTenantId(BizDataInterface.getBean().currentUserTenantId());
        return accountDao.selectOne(o);
    }

    private Ccls parseCcls(EmailMessage message) {
        Ccls ccls = new Ccls();
        try {
            List<HashMap<String, String>> ls = mapper.readValue(message.getRecipientCcls(), new ArrayList<HashMap<String, String>>().getClass());
            ls.forEach(i -> ccls.add(new Cc(i.get("recipient"), i.get("mail"))));
        } catch (IOException e) {
            throw new CclsInvalidFormatException();
        }
        return ccls;
    }

}
